﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UiManager : MonoBehaviour {
	public static UiManager instance;
	public GameObject shopBallContent;
	public GameObject mapBallContent;
	public GameObject charShop;
	public GameObject mapShop;
	public GameObject pausePanel;
	public GameObject gameOverPanel;
	public ShopItem ballShopPrefabs;
	public ShopItem mapShopPrefabs;
	public GameObject mainMenu;
	public GameObject timeSlider;
	public Sprite[] ballIconSprite;
	public Sprite[] mapIconSprite;
	public Sprite pauseSprite;
	public Sprite soundSprite;
	public Button startButton;
	public Button shopCharButton;
	public Button shopMapButton;
	public Button shopCharBackButton;
	public Button shopMapBackButton;
	public Button pauseButton;
	public Button soundButton;
	public Button homButton;
	public Button backButton;
	public Button playButton;
	public Button homeButton;
	public static GameObject tempBallSelect;
	public static GameObject tempMapSelect;
	public Text coinText;
	public Text highScoreText;
	public Text highScoreTextGO;
	public Text gameScoreText;
	public Text gameScoreTextGO;

	void Awake()
	{
		instance = this;
	}

	void Start () {
		ShopBallIconSet ();
		startButton.onClick.AddListener (StartGame);
		shopCharButton.onClick.AddListener (CharacterShop);
		shopMapButton.onClick.AddListener (MapShop);
		shopCharBackButton.onClick.AddListener (ShopBack);
		shopMapBackButton.onClick.AddListener (ShopBack);
		pauseButton.onClick.AddListener (PauseOrSound);
		backButton.onClick.AddListener (Resume);
		homButton.onClick.AddListener (Home);
		playButton.onClick.AddListener (StartGame);
		homeButton.onClick.AddListener (Home);
		soundButton.onClick.AddListener (AudioManager.SetAudio);
		AdsControl.Instance.ShowBanner ();
	}
	public static void InItText()
	{
		SetCoinText (false,0);
		SetScoreText (false);
	}
	public static void SetScoreText(bool addScore)
	{
		if (addScore) {
			GameStatistic.AddScore (1);
			GameStatistic.SetHighScore();
		}
		else
			GameStatistic.AddScore (0);
		instance.gameScoreText.text = GameStatistic.GameScore.ToString ();
		instance.gameScoreTextGO.text = GameStatistic.GameScore.ToString ();
		instance.highScoreText.text = GameStatistic.HighScore.ToString ();
		instance.highScoreTextGO.text = GameStatistic.HighScore.ToString ();
	}
	public static void SetCoinText(bool addScore, int coinValue)
	{
		if (addScore)
			GameStatistic.AddCoin (coinValue);
		instance.coinText.text =  GameStatistic.Coin.ToString();
	}
	private void ShopBallIconSet()
	{
		Transform[] totalItem = shopBallContent.transform.GetComponentsInChildren <Transform>();
		for (int i = 0; i <27; i++) {
			var ball = ballShopPrefabs.Spawn (shopBallContent.transform);
			ball.type = ItemType.BALL;
			ball.transform.localScale = Vector3.one * 0.3f;
			ball.index = i;
			ball.img.sprite = ballIconSprite [i];
			ball.price = GameStatistic.instance.ballPrice [ball.index].price;
			ball.priceText.text =  /*GameStatistic.Coin.ToString ()+ " / " +*/ ball.price.ToString ();
		}
		for (int i = 0; i < 4; i++) {
			var map = mapShopPrefabs.Spawn (mapBallContent.transform);
			map.type = ItemType.MAP;
			map.index = i;
			map.img.sprite = mapIconSprite [i];
			map.price = GameStatistic.instance.mapPrice [map.index].price;
			map.priceText.text =/* GameStatistic.Coin.ToString ()+ " / " + */map.price.ToString () ;
		}
	}
	public static void GameOver()
	{
		instance.gameOverPanel.SetActive (true);
	}
	public static void Home()
	{
		Time.timeScale = 1;
		instance.mainMenu.SetActive (true);
		instance.pausePanel.SetActive (false);
		instance.timeSlider.SetActive (false);
		instance.gameScoreText.gameObject.SetActive (false);
		instance.gameOverPanel.SetActive (false);
		instance.pauseButton.GetComponent<Image> ().sprite = instance.soundSprite;
		ClockAnimation.Reset ();
		TimeSlider.Reset ();
		GameStatistic.AddScore (-1);
		AudioManager.CheckAudioStage ();
		SetScoreText (false);
		AudioManager.PlayOneShoot (5);
		GameManager.gameStage = GameStage.WAIT;
	}
	private void StartGame()
	{
		mainMenu.SetActive (false);
		timeSlider.SetActive (true);
		gameOverPanel.SetActive (false);
		gameScoreText.gameObject.SetActive (true);
		pauseButton.GetComponent<Image> ().sprite = pauseSprite;
		GameStatistic.AddScore (-1);
		SetScoreText (false);
		TimeSlider.Reset ();
		ClockAnimation.Reset ();
		AudioManager.PlayOneShoot (5);
		GameManager.gameStage = GameStage.PLAY;
	}
	public static void PauseOrSound()
	{
		if (GameManager.gameStage == GameStage.PAUSE || GameManager.IsPlaying) {
			instance.pausePanel.SetActive (true);
			AudioManager.PlayOneShoot (5);
			GameManager.gameStage = GameStage.PAUSE;
		} else {
			AudioManager.SetAudio ();
		}
	}
	public static void Resume()
	{
		instance.pausePanel.SetActive (false);
		AudioManager.PlayOneShoot (5);
		GameManager.gameStage = GameStage.RESUME;
	}

	private void CharacterShop()
	{
		charShop.SetActive (true);
		mapShop.SetActive (false);
		AudioManager.PlayOneShoot (5);
	}
	private void MapShop()
	{
		charShop.SetActive (false);
		mapShop.SetActive (true);
		AudioManager.PlayOneShoot (5);
	}
	private void ShopBack()
	{
		charShop.SetActive (false);
		mapShop.SetActive (false);
		AudioManager.PlayOneShoot (5);
	}
}
